package org.hao.samples.service;

import java.util.ArrayList;
import java.util.List;

import org.hao.samples.dao.IExamScoreDAO;
import org.hao.samples.dao.IStudentDAO;
import org.hao.samples.dao.ISubjectDAO;
import org.hao.samples.dao.IUserSubDAO;
import org.hao.samples.model.ExamScore;
import org.hao.samples.model.ExamUserSubject;
import org.hao.samples.model.OneSubjectVO;
import org.hao.samples.model.Student;
import org.hao.samples.model.Subject;
import org.hao.samples.model.SubjectVO;
import org.hao.samples.model.scoreVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class subjectServiceImpl implements subjectService {

	@Autowired
	private ISubjectDAO dao;

	@Autowired
	private IStudentDAO studentDAO;

	@Autowired
	private IUserSubDAO UserSubjectdao;

	@Autowired
	private IUserSubDAO userSubDAO;

	@Autowired
	private IExamScoreDAO examScoreDAO;

	@Override
	public List<Subject> findAllSubject() {
		return dao.findAllsubject();
	}

	@Override
	public void createsubject(Subject subject) throws Exception {
		dao.createsubject(subject);
	}

	@Override
	public void delete(Integer id) throws Exception {
		dao.deletesubject(id);
	}

	@Override
	public Subject getsubjectById(Integer id) {
		return dao.getsubjectById(id);
	}

	@Override
	public void editsubject(Subject subject) throws Exception {
		dao.editsubject(subject);
	}

	@Override
	public void saveExamTest(SubjectVO subjectVO) throws Exception {
		Subject subject = new Subject();
		BeanUtils.copyProperties(subjectVO, subject);
		dao.createsubject(subject);
	}

	@Override
	public void editTest(SubjectVO subjectVO) throws Exception {
		Subject subject = new Subject();
		BeanUtils.copyProperties(subjectVO, subject);
		dao.editsubject(subject);
		;

	}

	@Override
	public SubjectVO getSubjectVOById(Integer id) {
		Subject subject = dao.getsubjectById(id);
		SubjectVO subjectVO = new SubjectVO();
		BeanUtils.copyProperties(subject, subjectVO);
		return subjectVO;
	}

	@Override
	public List<Student> getAllUserBySubject(Integer subjectId) {
		return studentDAO.getAllUserBySubject(subjectId);
	}

	@Override
	public List<SubjectVO> findAllSubjectVO() {
		List<Subject> subject = dao.findAllsubject();
		ArrayList<SubjectVO> subjectList = new ArrayList<SubjectVO>();
		for (Subject a : subject) {
			SubjectVO vo = new SubjectVO();
			BeanUtils.copyProperties(a, vo);
			subjectList.add(vo);
		}
		return subjectList;
	}

	@Override
	public void saveSubjectStudent(List<Integer> list, Integer subjectId) {
		// subjectId是当前科目ID,list是重新被选中的学生Id

		//目前被选中的学生
		List<Student> studentList = studentDAO.getAllUserBySubject(subjectId);
		List<Integer> dbSubjectUserList = new ArrayList<Integer>();
		for (int i = 0; i < studentList.size(); i++) {
			dbSubjectUserList.add(studentList.get(i).getUser_id());
		}

		// 应该新增的学生
		ArrayList<Integer> insertList = new ArrayList<Integer>();

		// 应该删除的学生
		ArrayList<Integer> delList = new ArrayList<Integer>();

		for (Integer selectedUser : list) {
			if (!dbSubjectUserList.contains(selectedUser)) {
				insertList.add(selectedUser);
			}
		}

		for (Integer selectedUser : dbSubjectUserList) {
			if (!list.contains(selectedUser)) {
				delList.add(selectedUser);
			}
		}

		for (Integer insert : insertList) {
			Student student = new Student();
			student.setUser_id(insert);
			Subject subject = new Subject();
			subject.setSub_id(subjectId);
			ExamUserSubject examUserSubject = new ExamUserSubject(student, subject);
			UserSubjectdao.insertSubjectUsers(examUserSubject);
		}

		for (Integer insert : delList) {
			Student student = new Student();
			student.setUser_id(insert);
			Subject subject = new Subject();
			subject.setSub_id(subjectId);
			ExamUserSubject examUserSubject = new ExamUserSubject(student, subject);
			UserSubjectdao.deleteSubjectUsers(examUserSubject);
		}
		// UserSubjectdao.insertSubjectUsers(insertList,subjectId);
		// UserSubjectdao.deleteSubjectUsers(insertList,subjectId);
	}

	@Override
	public List<OneSubjectVO> findAllSubjectByStudent(Integer userId) {
		// 获取当前学生所有的科目
		List<ExamUserSubject> list = userSubDAO.findSubjectUsersById(userId);
		List<OneSubjectVO> listvo = new ArrayList<OneSubjectVO>();
		for (ExamUserSubject l : list) {
			OneSubjectVO vo = new OneSubjectVO();
			BeanUtils.copyProperties(l.getSubject(), vo);
			listvo.add(vo);
		}

		// 获取当前学生已参考的科目
		List<ExamScore> list2 = examScoreDAO.findExamScoreBystuId(userId);
		for (OneSubjectVO osj : listvo) {
			for (ExamScore l : list2) {
				if (osj.getSub_id() == l.getSubject().getSub_id()) {
					osj.setScore(l.getScore());
					osj.setTestStatus(true);
					osj.setScoreId(l.getId());
				}
			}
		}
		return listvo;
	}

	@Override
	public scoreVO findScoreById(Integer id, String userName) {
		ExamScore sc = examScoreDAO.findExamScoreById(id);
		scoreVO vo = new scoreVO();
		vo.setExamTime(sc.getExamTime().toString());
		vo.setScore(sc.getScore());
		vo.setSubjectName(sc.getSubject().getName());
		vo.setUserName(userName);
		return vo;
	}

}
